package com.tom;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AddPostServlet
 */
public class AddPostServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddPostServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                request.setCharacterEncoding("UTF-8");
//              String userid = request.getParameter("userid");
//              String nickname = request.getParameter("nickname");
                String title = request.getParameter("title");
                String content = request.getParameter("content");
                HttpSession session = request.getSession();

                Member m = (Member) session.getAttribute("m");

                Post p = new Post(m.getUserid(), m.getNickname(), title, content);
                
                session.setAttribute("p", p);
                boolean saved = p.save();
                if (saved){
                        //成功
                        
                }else{
                        //留言新增失敗
                }
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                doGet(request, response);
        }

}